﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using System.Data.SqlClient;
using System.IO;
public partial class Manager : System.Web.UI.Page
{

    static string path;
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
        {
            if (Session["admin"] == null)
            {

                Response.Redirect("Default.aspx");

            }
            string name = Session["admin"].ToString();
          
            string isq = Request .QueryString ["q"].ToString();

            if (isq == "aa")
            {
                Griddir.DataSource = new DirInfoManager().FindTable(name, 0);
                Griddir.DataBind();
                Gridfile.DataSource = new FileInfoManager().FindTable(name, 0);
                Gridfile.DataBind();
                path = name;
                Labpath.Text = path;
            }
            else
            {
                int myparentid = Convert.ToInt32(Request.QueryString["p"].ToString());
                
                string dirname = Server.UrlDecode (Request.QueryString["dd"].ToString());
                int parentid = new DirInfoManager().FindId(name, myparentid, dirname);

                Griddir.DataSource = new DirInfoManager().FindTable(name, parentid);
                Griddir.DataBind();
                Gridfile.DataSource = new FileInfoManager().FindTable(name, parentid);
                Gridfile.DataBind();
                string[] mypath = new string[20];
                int i=0;
                DataTable dt = new DataTable();
                while (myparentid != 0)
                {
                    dt = new DirInfoManager().Findmy(name, myparentid);
                    myparentid = Convert .ToInt32 (dt.Rows[0][2].ToString());
                   
                    mypath[i] = dt.Rows[0][3].ToString();
                    i = i + 1;
                }
                string mindpath=string .Empty ;
                for (int j=i-1; j >=0; j--)
                {
                    mindpath = mindpath +"\\"+ mypath[j];
                   
                }

                Labpath.Text = name + mindpath + "\\" + dirname;



            }
       }
    }
    protected void lbtndeldir_Click(object sender, EventArgs e)
    {
         string name = Session["admin"].ToString();
        LinkButton lb = (LinkButton)sender;
        string[] array = lb.CommandArgument.Split(',');
        int dirId =Convert .ToInt32 ( array [0]);
        string   dirname2 = array[1];
        string path = Server.MapPath("Upfile" + "\\" + Labpath.Text.ToString() + "\\" + dirname2);

        bool b = new DirInfoManager().Deletebyid(name, dirId);
        if (b)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            di.Delete(true);

            string isq = Request.QueryString["q"].ToString();

            if (isq == "aa")
            {
                Griddir.DataSource = new DirInfoManager().FindTable(name, 0);
                Griddir.DataBind();
                Gridfile.DataSource = new FileInfoManager().FindTable(name, 0);
                Gridfile.DataBind();
            }
            else
            {
                int myparentid = Convert.ToInt32(Request.QueryString["p"].ToString());

                string dirname = Server.UrlDecode(Request.QueryString["dd"].ToString());
                int parentid = new DirInfoManager().FindId(name, myparentid, dirname);

                Griddir.DataSource = new DirInfoManager().FindTable(name, parentid);
                Griddir.DataBind();
                Gridfile.DataSource = new FileInfoManager().FindTable(name, parentid);
                Gridfile.DataBind();
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除失败，请联系管理员！');</script>");
        }

    }
    protected void lbtndelfile_Click(object sender, EventArgs e)
    {
        string name = Session["admin"].ToString();
        LinkButton lb = (LinkButton)sender;
        string []array=lb.CommandArgument.Split (',');
        int fileId = Convert.ToInt32(array [0]);
        string filename=array [1];
        string fileext=array [2];
        string path=Server .MapPath ("Upfile"+"\\"+Labpath .Text .ToString ()+"\\"+filename +fileext );
        bool b = new FileInfoManager().Deletebyid(name, fileId);
        if (b)
        {
            FileInfo fi = new FileInfo(path);
            fi.Delete();
            string isq = Request.QueryString["q"].ToString();

            if (isq == "aa")
            {
                Griddir.DataSource = new DirInfoManager().FindTable(name, 0);
                Griddir.DataBind();
                Gridfile.DataSource = new FileInfoManager().FindTable(name, 0);
                Gridfile.DataBind();
            }
            else
            {
                int myparentid = Convert.ToInt32(Request.QueryString["p"].ToString());

                string dirname = Server.UrlDecode(Request.QueryString["dd"].ToString());
                int parentid = new DirInfoManager().FindId(name, myparentid, dirname);

                Griddir.DataSource = new DirInfoManager().FindTable(name, parentid);
                Griddir.DataBind();
                Gridfile.DataSource = new FileInfoManager().FindTable(name, parentid);
                Gridfile.DataBind();
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除失败，请联系管理员！');</script>");
        }

    }



    protected void Ibnew_Click(object sender, ImageClickEventArgs e)
    {
        string isq = Request.QueryString["q"].ToString();
        string newpath = Server.UrlEncode(Labpath.Text.ToString());
        
        if (isq == "aa")
        {
            Response.Redirect("Newdir.aspx?path=" + newpath + "&q=" + isq);
        }
        else
        {
            int myparentid = Convert.ToInt32(Request.QueryString["p"].ToString());
            string dirname = Request.QueryString["dd"].ToString();
           
            Response.Redirect("Newdir.aspx?path=" + newpath + "&dd="+dirname +"&p="+myparentid +"&q=pp");



        }
    }
    protected void down_Click(object sender, EventArgs e)
    {
        string name=Session ["admin"].ToString ();
        LinkButton lb = (LinkButton)sender;
        string [] arr = lb.CommandArgument.Split(',');
        string filename =  Server .UrlEncode (arr[0]+arr[1]);
        string la = Labpath.Text.ToString();
        string path = Server .UrlEncode (Server.MapPath ("Upfile"+"\\"+la+ "\\" + filename));
        Response.Redirect("Down.aspx?filename=" + filename + "&path=" + path);
          
        
     


    }
    protected void Ibhome_Click(object sender, ImageClickEventArgs e)
    {

        Response.Redirect("../Web/Manager.aspx?p=0&q=aa");
    }
    protected void lbtncmmfile_Click(object sender, EventArgs e)
    {
        string name = Session["admin"].ToString();
        LinkButton lb = (LinkButton)sender;
        string[] array = lb.CommandArgument.Split(',');
        int fileid = Convert.ToInt32(array[0]);
        string filename =( array[1]);
        string fileext = array[2];
    
        string labpath=Server .UrlEncode (Labpath.Text.ToString()) ;

        string q=Request.QueryString["q"].ToString ();
        string p=Request .QueryString ["p"].ToString ();
        if (q != "aa")
        {
            string dd = Request.QueryString["dd"].ToString();
            Response.Redirect("Rechristen.aspx?fileid=" + fileid + "&dirid=0" + "&q=" + q + "&p=" + p + "&dd=" + dd + "&fileext=" + fileext + "&labpath=" + labpath + "&filename=" + filename);
        }
        else
        {
            Response.Redirect("Rechristen.aspx?fileid=" + fileid +  "&dirid=0" + "&q=aa" + "&p=" + p + "&dd=0" + "&fileext=" + fileext + "&labpath=" + labpath + "&filename=" + filename);
        }

    }

    protected void lbtncmmdir_Click(object sender, EventArgs e)
    {
        string name = Session["admin"].ToString();
        LinkButton lb = (LinkButton)sender;
        string[] array = lb.CommandArgument.Split(',');
        int dirid = Convert.ToInt32(array[0]);
        string dirname = (array[1]);
      

        string labpath = Server.UrlEncode(Labpath.Text.ToString());

        string q = Request.QueryString["q"].ToString();
        string p = Request.QueryString["p"].ToString();
        if (q != "aa")
        {
            string dd = Request.QueryString["dd"].ToString();
            Response.Redirect("Rechristen.aspx?dirid=" + dirid + "&fileid=0" + "&q=" + q + "&p=" + p + "&dd=" + dd +  "&labpath=" + labpath + "&dirname=" + dirname);
        }
        else
        {
            Response.Redirect("Rechristen.aspx?dirid=" +dirid + "&fileid=0" + "&q=aa" + "&p=" + p + "&dd=0"  + "&labpath=" + labpath + "&dirname=" + dirname);
        }

    }




    protected void lbupload_Click(object sender, ImageClickEventArgs e)
    {
        string path =Server .UrlEncode ( Labpath.Text.ToString());
        string q = Request.QueryString["q"].ToString();
        string p = Request.QueryString["p"].ToString();
        if (q != "aa")
        {
            string dd = Request.QueryString["dd"].ToString();
            Response.Redirect("Fileupload.aspx?path=" + path + "&q=" + q + "&dd=" + dd + "&p=" + p);
       
        }
        else
        {

            Response.Redirect("Fileupload.aspx?path=" + path + "&q=aa");
        }
    }
}
